package view.hero
{
import core.Language;
import core.StageProxy;
import core.model.PlayerModel;

import flash.display.MovieClip;

import values.avatar.IAvatarValue;
import values.events.AvatarForceEvent;

import view.component.window.WindowProxy;

/**
 * HeroUIProxy
 * @author jliang
 *
 */
public class HeroUIProxy extends WindowProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function HeroUIProxy()
	{
		super();
	}	
	//==========================================================================
	//  Properties
	//==========================================================================
	[Inject]
	public var playerModel:PlayerModel;
	
	public function get heroList():HeroListProxy
	{
		return proxys["heroList"];
	}
	
	public function get heroDetail():HeroDetailUIProxy
	{
		return proxys["heroDetail"];
	}
	
	protected var inited:Boolean = false;
	//==========================================================================
	//  Overridden methods: WindowProxy
	//==========================================================================
	override protected function get proxyMapping():Array
	{
		return [ 
			{ skin:"heroList", classRef: HeroListProxy, id:"heroList" },
			{ skin:"heroDetail", classRef: HeroDetailUIProxy, id:"heroDetail" }
		].concat(super.proxyMapping);
	}
	
	override public function parse(target:MovieClip):void
	{
		super.parse(target);
		
		skin.titleLabel.text = Language.getText("@avatar_info_label");
		
		heroList.onHeroSelectedHandler = onHeroSelectedHandler;
		
		playerModel.myForce.addEventListener(AvatarForceEvent.HIRE_HERO , heroChangedHandler);
		playerModel.myForce.addEventListener(AvatarForceEvent.ABANDON_HERO , heroChangedHandler);
	}
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function showHero(avatar:IAvatarValue = null):void
	{
		if (!inited) refresh();
		
		show();
		
		if (avatar) heroList.selectHero(avatar);
	}
	
	public function refresh():void
	{
		heroList.setData(playerModel.myForce.allHeros);
		
		inited = true;
	}
	//==========================================================================
	//  Protected methods
	//==========================================================================	
	protected function onHeroSelectedHandler(avatar:IAvatarValue):void
	{
		heroDetail.data = avatar;
	}
	
	protected function heroChangedHandler(event:AvatarForceEvent):void
	{
        StageProxy.unlock();
		refresh();
	}
}
}


